package pers.kw.config.feign;

import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import pers.kw.exceptions.BizException;

/**
 * 处理feign调用，响应码非200的返回值
 * feign异常
 */
@Configuration
@Slf4j
public class FeignErrorDecoder implements ErrorDecoder {
    @Override
    public Exception decode(String s, Response response) {
        String body = "";
        if (response != null && response.body() != null) {
            try {
                body = Util.toString(response.body().asReader());
                log.error(s, body);
            } catch (Exception e) {
                log.error("FeignErrorDecoder异常", e);
            }
        }
        return new BizException(body);
    }
}
